package written_test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * @author zhx
 * @since 2020-09-03
 */
public class BaiDu {

    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        int sum = Arrays.stream(arr).sum();
        int countOfFive = sum / 5;
        int countOfZero = n - countOfFive;
        if (countOfZero == 0) {
            System.out.println(-1);
            System.exit(0);
        }

        for (int ofZero = countOfZero; ofZero > 0; ofZero--) {
            for (int ofFive = countOfFive; ofFive > 0; ofFive--) {
                long number = getNumber(ofFive, ofZero);
//                if (number % 90 ==0){
//                    System.out.println(number);
//                    System.exit(0);
//                }
            }
        }

    }

    public static long getNumber(int lengthOfFive, int lengthOfZero) {
        StringBuffer sb = new StringBuffer();
        List<Long> results = new ArrayList<>();
        for (int i = 0; i < lengthOfFive; i++) {
            sb.append(5);
        }
        int offset = lengthOfFive - 1;
        return Long.parseLong(sb.toString());
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for (int i = 0; i < T; i++) {
            getCow(in);
        }
        in.close();
    }

    public static void getCow(Scanner in) {
        int n = in.nextInt();
        int m = in.nextInt();
        List<Integer> result = new ArrayList<>();
        List<Integer> tmp = new ArrayList<>();
        List<Integer> tmp1 = new ArrayList<>();
        if (m == 1) {
            int k = in.nextInt();
            for (int i = 0; i < k; i++) {
                int l = in.nextInt();
                int r = in.nextInt();
                for (int j = l; j <= r; j++) {
                    result.add(j);
                }
            }
            System.out.println(result.size());
            result.forEach(System.out::println);
        } else {
            for (int i = 0; i < m; i++) {
                int k = in.nextInt();
                for (int index = 0; index < k; index++) {
                    int l = in.nextInt();
                    int r = in.nextInt();
                    for (int j = l; j <= r; j++) {
                        if (i == 0) {
                            tmp.add(j);
                            tmp1.add(j);
                        } else {
                            tmp1.add(j);
                        }
                    }
                }
                tmp.addAll(result);
                result.clear();
                for (Integer intTmp : tmp) {
                    if (tmp1.contains(intTmp)) {
                        result.add(intTmp);
                    }
                }
                tmp1.clear();
                tmp.clear();
            }
            System.out.println(result.size());
            result.forEach((integer)->{
                System.out.print(integer+" ");
            });
            System.out.println();
        }
    }


    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int lastStep = 0;
        int lastOfLastStep = 0;
        int[] dp = new int[n];
        if (n == 1) {
            System.out.println(1);
            System.exit(0);
        }
        dp[0] = 1;
        if (n == 2) {
            System.out.println(1);
            System.exit(0);
        }
        dp[1] = 1;

        for (int i = 2; i < n; i++) {

        }
    }
}
